package cn.ex.system.controller;

import cn.ex.basic.util.AjaxResult;
import cn.ex.basic.util.PageList;
import cn.ex.system.domain.DictionaryType;
import cn.ex.system.query.DictionaryTypeQuery;
import cn.ex.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ZeWangmu
 * @Date: 2022/06/10/10:01
 * @Description:
 */
@RestController
@RequestMapping("/type")
@Api(value = "数据字典的API",description="数据字典相关的CRUD功能")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.findAll();
    }
    /**
     * 查询一条
     */
    @GetMapping("/{id}")
    public DictionaryType findById(@PathVariable("id") Long id){
        return  dictionaryTypeService.findById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");

        }
    }

    /**
     * 高级+分页查询
     * @param dectionaryDetailQuery
     * @return
     */
    @PostMapping
    public PageList<DictionaryType> query(@RequestBody DictionaryTypeQuery dectionaryDetailQuery){
        return dictionaryTypeService.queryPage(dectionaryDetailQuery);
    }

    /**
     * 新增或修改
     * @param DictionaryType
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody DictionaryType DictionaryType) {
        try {
            if (DictionaryType.getId() == null) {
                dictionaryTypeService.save(DictionaryType);
            } else {
                dictionaryTypeService.update(DictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            dictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }
}
